﻿using CaltechSoft.Service.EntityFW.Context;
using CaltechSoft.Service.EntityFW.Entity;
using CaltechSoft.Service.Infrastructure.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaltechSoft.Service.CommandService
{
    public class CertificateDetailsCommandService : ICertificateDetailsCommandService
    {
        public int CreateCertificateDetails(int certificateId, int certificateStandardId)
        {
            Guard.ArgumentIsNotNull(certificateId, "command");

            using (var context = new CaltechContext())
            {
                var existingItem = context.Set<CertificateDetails>().FirstOrDefault(n => n.CertificateId == certificateId &&
                                                                                     n.CertificateStandardId == certificateStandardId
                                                                                    );
                if (existingItem != null)
                {
                    return existingItem.CertificateStandardId;
                }
                var cert = new CertificateDetails
                {
                    CertificateId = certificateId,
                    CertificateStandardId = certificateStandardId
                };
                context.Set<CertificateDetails>().Add(cert);
                context.SaveChanges();
                return cert.CertificateDetailsId;
            }
        }
    }
}
